package seGobangClient.uiSystem3D;

import java.util.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.behaviors.vp.*;
import com.sun.j3d.utils.geometry.Text2D;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import seGobangClient.ISceneController;
import seGobangClient.Options;
import seGobangClient.Rule;
import seGobangClient.gaming.IGamingScene;
import seGobangClient.gaming.IPlayer;

/**
 *
 * @author plus
 */
public class SceneManager implements seGobangClient.IUISystem,
        seGobangClient.IMessageBox, seGobangClient.mainMenu.IMainMenuScene,
        seGobangClient.gaming.IGamingScene , seGobangClient.configMenu.IConfigScene{
    
    static seGobangClient.ISceneController MyCurrentController;                 //场景控制器
    private static HashMap<String, IScene3D> ScenePool = new HashMap<String, IScene3D>();       //子场景池
    private static MessageBox3D MessageBox=new MessageBox3D();
    private static IScene3D CurrentScene3D;
    static Canvas3D canvas;
    static PlatformGeometry dashBoardRoot;
    static BranchGroup objRoot;
    private static BranchGroup globalGroup;
    private static SimpleUniverse universe;
    private static JFrame Container;
    private static Text2D textHelp;
    static boolean bEnable=true;    //显示MessageBox时用于屏蔽操作

    public SceneManager(javax.swing.JFrame Container) {
        this.Container=Container;
        Container.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        Container.addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent e)
            {
                SceneManager.MyCurrentController.doEvent("ExitGame", null);
            }
        });
        initJ3D(Container);
        initView();
        initScenePool();
    }

    private void initJ3D(javax.swing.JFrame Container) {
        canvas = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
        Container.setLayout(new java.awt.BorderLayout());
        Container.add("Center", canvas);
        
        universe = new SimpleUniverse(canvas);

        objRoot = new BranchGroup();
        objRoot.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
        //objRoot.setCapability(Group.ALLOW_CHILDREN_READ);
        objRoot.setCapability(Group.ALLOW_CHILDREN_WRITE);

        dashBoardRoot = new PlatformGeometry();
        dashBoardRoot.setCapability(PlatformGeometry.ALLOW_CHILDREN_EXTEND);
        dashBoardRoot.setCapability(PlatformGeometry.ALLOW_CHILDREN_WRITE);

        initLight();
        initGlobalGroup();

        objRoot.compile();
        dashBoardRoot.compile();

        universe.addBranchGraph(objRoot);
        universe.getViewingPlatform().setPlatformGeometry(dashBoardRoot);
    }

    private void initGlobalGroup() {
        globalGroup = new BranchGroup();
        globalGroup.addChild(ObjectFactory.createRoom());
        globalGroup.addChild(ObjectFactory.createChessboard());
        globalGroup.setName("Global");
        //globalGroup.setCapability(BranchGroup.ALLOW_DETACH);
        globalGroup.compile();
        objRoot.addChild(globalGroup);
        
        textHelp=new Text2D("我是华丽的帮助信息!",new Color3f(1f,1f,1f),"微软雅黑",24,java.awt.Font.PLAIN);
        dashBoardRoot.addChild(MessageBox3D.getTG(textHelp,-0.22f,-0.18f,-0.6f));
    }

    private void initScenePool() {
        ScenePool.put("MainMenu", new MainMenuScene3D());
        ScenePool.put("Gaming", new GamingScene3D());
        ScenePool.put("ConfigManu", new Config3D(Container,new getpane(800,568,"")));
    }

    private void initLight() {
        AmbientLight ALight = new AmbientLight(true, new Color3f(0.3f, 0.3f, 0.3f));
        Light Dlight = new PointLight(true, new Color3f(0.6f, 0.6f, 0.6f),
                new Point3f(10f, 10f, 10f), new Point3f(1f, 0, 0));
        Dlight.setInfluencingBounds(new BoundingSphere(new Point3d(0d, 0d, 0d), 100));
        Light Dlight2 = new PointLight(true, new Color3f(0.1f, 0.1f, 0.1f),
                new Point3f(-10f, 0f, -10f), new Point3f(1f, 0, 0));
        Dlight2.setInfluencingBounds(new BoundingSphere(new Point3d(0d, 0d, 0d), 100));
        //objRoot.addChild(ALight);
        objRoot.addChild(Dlight);
        objRoot.addChild(Dlight2);
    }

    static void setView(Vector3f pos, Vector3f rot){
        Transform3D t3d = new Transform3D(), t3drx = new Transform3D(),
                    t3dry = new Transform3D(),t3drz = new Transform3D();
        t3d.set(pos);
        t3drx.rotX(rot.x);
        t3dry.rotY(rot.y);
        t3drz.rotZ(rot.z);
        t3drx.mul(t3dry);
        t3drx.mul(t3drz);
        t3d.mul(t3drx);
        universe.getViewingPlatform().getViewPlatformTransform().setTransform(t3d);
    }
    
    private void initView() {
        OrbitBehavior orbit = new OrbitBehavior(canvas,
                OrbitBehavior.REVERSE_ALL | OrbitBehavior.STOP_ZOOM);
        orbit.setSchedulingBounds(new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 1000.0));

        Transform3D t3d = new Transform3D(), t3dr = new Transform3D();
        t3d.set(new Vector3f(0, 0, 25));
        t3dr.rotX(-Math.PI / 2);
        t3dr.mul(t3d);
        universe.getViewingPlatform().getViewPlatformTransform().setTransform(t3dr);
        universe.getViewer().getView().setBackClipDistance(100.0);
        universe.getViewer().getView().setFrontClipDistance(0.001);
        universe.getViewingPlatform().setViewPlatformBehavior(orbit);
    //universe.getViewingPlatform().setViewPlatformBehavior(null);
    }

    public int showMessageBox(int Type, String Title, String Text, String CallBackEventName) {
        return MessageBox.showMessageBox(Type, Title, Text, CallBackEventName);
    }

    public void setCurrentController(ISceneController SController) {
        MyCurrentController = SController;
    }

    public int setHotItemID(int HotItemID) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void setChess(Point2i pos, byte ChessFlag) {
        ((IGamingScene)CurrentScene3D).setChess(pos, ChessFlag);
    }

    public void setChessBoardPickable(boolean isPickable) {
         ((IGamingScene)CurrentScene3D).setChessBoardPickable(isPickable);
    }

    public void setCurrentPlayer(byte ChessFlag) {
         ((IGamingScene)CurrentScene3D).setCurrentPlayer(ChessFlag);
    }

    public void updatePlayerInfo(IPlayer blackPlayer, IPlayer whitePlayer) {
         ((IGamingScene)CurrentScene3D).updatePlayerInfo(blackPlayer, whitePlayer);
    }

    public void initPlayerInfo(IPlayer blackPlayer, IPlayer whitePlayer) {
        ((IGamingScene)CurrentScene3D).initPlayerInfo(blackPlayer, whitePlayer);
    }

    public void AIFx(Point2i pos, int FxID) {
        ((IGamingScene)CurrentScene3D).AIFx(pos,FxID);
    }
    
    public void currentSceneChangeTo(String SceneName) {
        if (CurrentScene3D != null) {
            CurrentScene3D.shutDown();
        }
        CurrentScene3D = ScenePool.get(SceneName);
        CurrentScene3D.setUpScene();
    }
    
    public int getGraphic_Message() {
        return ((Config3D)CurrentScene3D).getGraphic_Message();
    }

    public int getMusicVolum_Message() {
        return ((Config3D)CurrentScene3D).getMusicVolum_Message();
    }

    public int getSFXVolum_Message() {
        return ((Config3D)CurrentScene3D).getSFXVolum_Message();
    }

    public int getTimeLimit_Message() {
        return ((Config3D)CurrentScene3D).getTimeLimit_Message();
    }

    public int getRegretTimes_Message() {
        return ((Config3D)CurrentScene3D).getRegretTimes_Message();
    }

    public void Options_perform(Options op) {
        ((Config3D)CurrentScene3D).Options_perform(op);
    }

    public void Rule_perform(Rule rl) {
        ((Config3D)CurrentScene3D).Rule_perform(rl);
    }

    public boolean getforbids_Message() {
        return ((Config3D)CurrentScene3D).getforbids_Message();
    }

    public void printHelpInfo(String helpInfo) {
        this.textHelp.setString(helpInfo);
    }
    
}
